<?php

namespace app\controller;

use app\BaseController;
use app\controller\model\apis\Hi;
use app\controller\model\apis\Tuanyou;
use app\controller\model\Config;
use app\controller\model\SPay;
use app\controller\model\Tables;
use think\facade\Session;
use think\facade\Db;

class SupplierNotify extends BaseController
{
	protected $d,$toName,$model,$tables,$orderNo,$order,$refundResult,$data,$reOrder,$o_pay_no,$table;
	public function __construct()
	{
		$this->d = explode("/",request()->url());
		$this->d = array_reverse($this->d,false);
		$this->toName = $this->d[0];
		$this->toName = explode("?",$this->toName)[0];
		$this->tables = new Tables();
	}
	public function tuan(){
	    $log = new \app\Log("tuanyou/notify");
		try {
			$post = request()->param();
			$supplier = DB::name($this->tables->supplier)->where("id",2)->find();
			$this->model =  new Tuanyou($supplier);
			$this->data = '';
			$this->data = $this->model->applyRefund($post);
			$log->write(['param'=>$post,'data'=> $this->data]);
			if($this->toName == 'applyRefund'){//发起退款成功通知
				if(is_array($this->data)){
					//退款成功
					//{"app_key":"appm_api100006691","orderSn":"CZBA377911440898401","outerOrderId":"JZX20250715151611175256377193304","refundAmount":"18176","refundTradeNo":"TY53905792725071518XD01","sign":"7f719dc3750023d45fa96528f3342ec7","timestamp":"1752570495940"}
					$this->orderNo = $this->data['outerOrderId'];//我们的订单号
					$this->o_pay_no = $this->data['orderSn'];//团油的支付订单号
					$this->reOrder = $this->data['refundTradeNo'];//退款订单号
					$this->refundResult = true;
					$this->pushOrder();
				}
			}
			if($this->toName == 'cancelRefund'){//取消退款或失败通知
				if(is_array($this->data)){
					//退款失败
					$this->orderNo = $this->data['outerOrderId'];//我们的订单号
					$this->o_pay_no = $this->data['orderSn'];//团油的支付订单号
					$this->reOrder = $this->data['refundTradeNo'];//退款订单号
					$this->refundResult = $this->data['rejectReason'];//失败原因
					$this->pushOrder();
				}
			}
			if($this->toName == 'getOrder'){//查询订单
				if(is_array($this->data)){
					$this->orderNo = $this->data['outerOrderId'];//我们的订单号
					$this->o_pay_no = $this->data['orderSn'];//团油的支付订单号
					$temarr=[
						'code'=>200,
						'message'=>'无消息',
						'result'=>[]
					];
					if(is_array($this->data)){
						if(!$this->getOrder()){
							$temarr['code']=500;
							$temarr['message']='订单不存在';
						}else{
							$result=3;
							if($this->table == $this->tables->serviceOrder){
								if($this->order['status']==0)$result=0;
								if($this->order['status']==1)$result=1;
								if($this->order['status']==-1)$result=2;
							}else{
								if($this->order['status']==2)$result=0;
								if($this->order['status']==3)$result=1;
								if($this->order['status']==-4)$result=2;
							}

							$temarr['result']=$result;
							$temarr['payAmount']=$this->order['o_amountPay'];
						}
					}
					$this->model->data = $temarr;
					$this->model->mkSign();
					exit(json_encode($this->model->data,JSON_UNESCAPED_UNICODE));
				// 	exit();
				}

			}
			if(is_array($this->data)){
				$temarr=[
					'code'=>200,
					'message'=>'无消息',
					'result'=>[]
				];
			}else{
				$temarr=[
					'code'=>500,
					'message'=>$this->data,
					'result'=>[]
				];
			}
			$this->model->data = $temarr;
			$this->model->mkSign();
			exit(json_encode($this->model->data ,JSON_UNESCAPED_UNICODE));
// 			exit();
		}catch (\Exception $e){
		    $log->write(['param'=>$post,'err'=>$e->getMessage()]);
			$temarr=[
				'code'=>500,
				'message'=>'内部数据错误',
				'result'=>[]
			];
			$this->model->data = $temarr;
			$this->model->mkSign();
			exit(json_encode($this->model->data ,JSON_UNESCAPED_UNICODE));
		}
	}
	public function Hi(){
		try {
			$post = request()->param();

			$file=fopen("./t_notify2.txt","w");
			fwrite($file,json_encode($post));
			fclose($file);


			$supplier = DB::name($this->tables->supplier)->where("id",4)->find();
			$this->model =  new Hi($supplier);
			if($this->toName == 'pushOrder'){
				$this->data = $this->model->pushOrder($post);

				if(!is_array($this->data)){
					echojson([],1,$this->data);
				}
				$this->orderNo = $this->data['orderNo'];
				$this->refundResult = ($this->data['refundResult']==true || $this->data['refundResult']=='true')?true:false;
				$this->pushOrder();
			}
			if($this->toName == 'pushGas'){
				$this->data = $this->model->pushGas($post);
			}
			echojson();
		}catch (\Exception $e){
			var_dump($e.error_get_last());
			echojson([],1,'数据错误:1');
		}
	}
	protected function getOrder(){
		$this->table = $this->tables->serviceOrder;
		$this->order = DB::name($this->table)->where("service_no",$this->orderNo)->find();
		if(!$this->order){
			$this->table = $this->tables->orders;
			$this->order = DB::name($this->table)->where("order_no",$this->orderNo)->find();
			if(!$this->order)return false;//无该订单 无法处理
		}
		return true;
	}
	protected function pushOrder(){
		$reModel =  new Orders(false);
		$this->getOrder();
		if( $this->refundResult===true){
			if($this->table == $this->tables->orders){
				if($this->order['reOrder']==3){
					$reModel->ordersDelRePay($this->order,false);
				}
			}
			if($this->table == $this->tables->serviceOrder){
				if($this->order['reOrder']==4){
					$reModel->serviceOrderDelRePay($this->order,false);
				}
			}
		}else{
			DB::name($this->table)->where("id",$this->order['id'])->update(['reOrder'=>-1,'reContent'=>$this->refundResult]);
			if($this->table == $this->tables->serviceOrder){//服务商需要发送反馈
				$pushRe = (new Service())->pushService($this->order['serviceID'],'order',$this->order['service_no']);
			}
		}
	}
}
